<?php
class arquivoComponent extends Component{

    
    public function show($model, $item) {
        $this->drawDestaque($item, 'file_label', 'file_descricao', 'adicionadoem');
        parent::show($model, $item);
    }
    
    private $images_ext = array('jpg', 'gif', 'png');
    public function showAnexos($model, $itens, $title = "", $class = ''){
        if(empty($itens)) return "";
        $this->LoadModel('usuario/login', 'uobj');
        $is_admin = $this->uobj->UserIsAdmin();
        $cod_usuario = $this->uobj->getCodUsuario();
        $this->LoadResource('upload', "up");
        $upurl   = $this->up->getDownloadUrl();
        $editurl = $this->Html->getLink('files/arquivo/edit');
        $this->Html->LoadCss('files');
        $id = str_replace("/", "_", $model);
        echo "<div$class> ";
            echo "<div class='title'>$title</div>";
            echo "<ul id='$id'>";
            foreach($itens as $item){
                
                extract($item);
                $img = "";
                $canedit = (!$is_admin && $item['cod_autor'] == $cod_usuario)?true:$is_admin;
                
                $visu_link = "<a href ='".URL_FILES. "$url$name"."' target='_blank'>Visualizar</a>";
                if(in_array($ext, $this->images_ext)){
                    $img_url = URL_FILES. "$url/thumb/$name";
                    $img = "<img src='$img_url' />";
                }else{
                    if($ext != 'pdf') $visu_link = "";
                    $img = $this->Html->LoadImage("filetypes/$ext.jpg", "", false);
                }
                
                $link = "$upurl?file=$cod_arquivo&folder=$folder";
                $size = files_arquivoModel::convertSizeUnity($size);
                $name = ($file_label     == "")?$name:$file_label;
                $desc = ($file_descricao == "")?'':$file_descricao;
                
                $var = $edit_link = $drop_link = "";
                $download_link = "<a href='$link' class='form_item download'>Download</a>";
                if($canedit){
                    $var = "
                     <div class='file_form'>
                        <form class='form hide' action='$editurl/$cod_arquivo' id='file_$cod_arquivo'>     
                            <span class='ldescricao'><b>Nome</b>: </span> <br/>
                            <input type='text' id='file_label' name='file_label' class='form_value' value='$name'/>
                            <span class='ldescricao'><b>Descrição</b>: </span> <br/>
                            <textarea type='text' id='file_descricao' name='file_descricao' class='form_value' cols='23' rows='4'>$desc</textarea>
                            <input type='hidden' id='ajax' name='ajax' value='true'/>
                        </form>
                     </div>
                    ";
                    $edit_link = " <a href='$editurl/$cod_arquivo' class='edit_file_form'>Editar</a> ";
                }
                
                if($is_admin){
                    $dropurl = $this->up->getDeleteUrl($cod_arquivo, $folder);
                    $drop_link = "<a href='$dropurl' class='drop_file'>Apagar</a>";
                }
                
                echo "
                    <li class='list-item list_$id'>
                        <div class='img'>$img</div>
                        <div class='file_content'>
                            <div class='name'>
                                <span class='lnome'><b>Nome</b>: </span>
                                <span class='form_item name'>$name</span>
                            </div>
                            <div class='descricao'>
                                <span class='ldescricao'><b>Descrição</b>: </span>
                                <span class='form_item descricao'>$desc</span>
                            </div>
                        </div>
                        $var
                        <div class='size'>Tamanho: $size</div>";
                         $this->gui->clear();
                        echo"<hr/><div class='file_actions'>$visu_link $download_link $edit_link $drop_link</div>";
                echo "</li>
                ";
            }
            echo "</ul>";
        echo "</div>"; 
        $this->gui->clear();
        $this->scripts();
        
    }
    
    public function scripts(){
        $this->Html->LoadJqueryFunction("
            
            if(typeof(arquivo) == 'undefined'){
                arquivo = '';
                $('.file_actions .drop_file').live('click', function(){
                    if(!confirm('Atenção o arquivo será excluído permanentemente, deseja prosseguir?')){return false;}
                    var url = $(this).attr('href');
                    var li = $(this).parent().parent();
                    $.ajax({
                        url: url,
                        type:'post',
                        success: function(response) {
                            if(response){
                                li.fadeOut('2500', function(){ li.remove(); });
                            }
                        }
                    });
                    return false;
                });
                
                $('.file_form form').live('submit', function(){
                    alert('clique no botão salvar!'); 
                    return false;
                });

                $('.edit_file_form').live('click', function(){
                    var temp  = $(this).parent().parent();
                    var edit  = $(this);
                    var form  = temp.children('.file_form').children('form');
                    var formid = form.attr('id');
                    var saved  = [];
                    value     = '';
                    
                    var file_content = temp.children('.file_content');
                    if(edit.html() == 'Editar'){
                        file_content.fadeOut('500', function(){
                            form.fadeIn('500');
                            edit.html('Salvar');
                        });
                    }else{
                        value     = form.children('.form_value').val();
                        var url   = edit.attr('href');
                        var label = form.children('.form_value').attr('id');
                        $.ajax({
                            url: url,
                            type:'post',
                            dataType: 'json',
                            data: form.serialize(),
                            success: function(response) {
                                form.fadeOut('500', function(){
                                    var filename        = form.children('#file_label').val();
                                    var filedescription = form.children('#file_descricao').val();
                                    file_content.children('.name').children('.name').html(filename);
                                    file_content.children('.descricao').children('.descricao').html(filedescription);
                                    file_content.fadeIn('500');
                                    edit.html('Editar');
                                });
                                if(response.status != 1){
                                    blockUI_error('As modificações não foram salvas');
                                }
                            }
                        });
                    }
                    return false;
                });
            }
        ");
    }
    
    public function format_size($size, $dados){
        return files_arquivoModel::convertSizeUnity($size);
    }
}
?>